package jia.core;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;

// ====================================================

/**
 * Definitions for compass directions
 *
 * @author Alec Lanter
 */
public class Compass {
    
    private static final Logger log = Logger.getLogger(Compass.class);
    
    public static final int NORTH_VAL = 0;
    public static final int SOUTH_VAL = 180;
    public static final int EAST_VAL = 90;
    public static final int WEST_VAL = 270;
    
    public static final Compass NORTH = new Compass("North", NORTH_VAL);
    public static final Compass EAST = new Compass("East", EAST_VAL);
    public static final Compass SOUTH = new Compass("South", SOUTH_VAL);
    public static final Compass WEST = new Compass("West", WEST_VAL);
    
    private int val;
    private String cardinalName;
    
    /**
     * Enforce non-instantiability
     */
    private Compass() {
        // GNDN
    }
    
    /**
     * @param dirVal
     */
    private Compass(String name, int dirVal) {
        this.cardinalName = name;
        this.val = dirVal;
    }
    
    /**
     * @return Returns the cardinalName.
     */
    public String getCardinalName() {
        return cardinalName;
    }
    
    /**
     * @return
     */
    public int getDirectionalValue() {
        return val;
    }
    
    /**
     * @param c1
     * @param c2
     * @return
     */
    public static Compass differenceBetween(Compass c1, Compass c2) {
        int diff = c1.getDirectionalValue() - c2.getDirectionalValue();
        diff = (diff + 360) % 360;
        switch (diff) {
            case NORTH_VAL:
                return NORTH;
            case SOUTH_VAL:
                return SOUTH;
            case EAST_VAL:
                return EAST;
            case WEST_VAL:
                return WEST;
            default:
                if (GenericFlags.errorMessages){
                    log.error("Can't find difference between Compass '"
                            + c1.getCardinalName() + "'("
                            + c1.getDirectionalValue() + ") and Compass '"
                            + c2.getCardinalName() + "'("
                            + c2.getDirectionalValue() + ")");
                }
                throw new RuntimeException("Can't find difference between Compass '"
                        + c1.getCardinalName() + "'("
                        + c1.getDirectionalValue() + ") and Compass '"
                        + c2.getCardinalName() + "'("
                        + c2.getDirectionalValue() + ")");
        }
    }
    
    /**
     * @param c1
     * @param c2
     * @return
     */
    public static Compass add(Compass c1, Compass c2) {
        int sum = c1.getDirectionalValue() + c2.getDirectionalValue();
        sum = sum % 360;
        switch (sum) {
            case NORTH_VAL:
                return NORTH;
            case SOUTH_VAL:
                return SOUTH;
            case EAST_VAL:
                return EAST;
            case WEST_VAL:
                return WEST;
            default:
                if (GenericFlags.errorMessages){
                    log.error("Can't find difference between Compass '"
                            + c1.getCardinalName() + "'("
                            + c1.getDirectionalValue() + ") and Compass '"
                            + c2.getCardinalName() + "'("
                            + c2.getDirectionalValue() + ")");
                }
                throw new RuntimeException("Can't find difference between Compass '"
                        + c1.getCardinalName() + "'("
                        + c1.getDirectionalValue() + ") and Compass '"
                        + c2.getCardinalName() + "'("
                        + c2.getDirectionalValue() + ")");
        }
    }
    
    /**
     * @param directionalValue
     * @return
     */
    public static Compass forDirectionalValue(int directionalValue) {
        switch (directionalValue) {
            case NORTH_VAL:
                return NORTH;
            case SOUTH_VAL:
                return SOUTH;
            case EAST_VAL:
                return EAST;
            case WEST_VAL:
                return WEST;
            default:
                return null;
        }
    }
}
